//
//  InfoViewController.m
//  SportRing
//
//  Created by 微他 on 15-1-19.
//  Copyright (c) 2015年 HotOne. All rights reserved.
//

#import "InfoViewController.h"
#import "UndertheFrame.h"
#import "PickerView.h"
#import "SRViewSetting.h"

@interface InfoViewController ()<UndertheFrameDelegate,UITextFieldDelegate,PickerViewDelegate>
{
    UIImageView *_imageView;
}
@property (weak, nonatomic) IBOutlet UIButton *avtarBtn;
@property (weak, nonatomic) IBOutlet UIButton *sexNan;
@property (weak, nonatomic) IBOutlet UIButton *sexNv;
//@property (weak, nonatomic) IBOutlet UITextField *nickName;
@property (weak, nonatomic) IBOutlet UILabel *age;
@property (weak, nonatomic) IBOutlet UILabel *height;
@property (weak, nonatomic) IBOutlet UILabel *weight;


@end

@implementation InfoViewController
- (IBAction)avtarClick:(UIButton *)sender {
    UndertheFrame *under = [[UndertheFrame alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)) isSex:NO];
    under.delegate = self;
    [self.view addSubview:under];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton = YES;
    [self addRightStringButton:NSLocalizedString(@"下一步", nil)];
    [self setCustomTitle:NSLocalizedString(@"个人信息",nil)];
    [self initView];
}

- (void)initView
{
    _avtarBtn.layer.cornerRadius = 40;
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 78, 78)];
    _imageView.image = [UIImage imageNamed:@"头像"];
    _imageView.center = _avtarBtn.center;
    _imageView.layer.cornerRadius = 39;
    _imageView.layer.masksToBounds = YES;
    _imageView.clipsToBounds = YES;
    [self.view addSubview:_imageView];
    
    _sexNan.layer.cornerRadius = 4;
    _sexNv.layer.cornerRadius = 4;
    _sexNan.selected = YES;
//    _nickName.delegate = self;
    SRViewSetting * setting = [SRViewSetting sharedSetting];
    setting.gender = SRGenderMale;
}

- (IBAction)chooseSex:(UIButton *)sender {
    sender.selected = YES;
    SRViewSetting * setting = [SRViewSetting sharedSetting];
    if (sender == _sexNan) {
        [sender setBackgroundColor:[UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1]];
        [_sexNv setBackgroundColor:[UIColor whiteColor]];
        _sexNv.selected = NO;
        [_sexNan setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_sexNv setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        setting.gender = SRGenderMale;
    }else
    {
        [sender setBackgroundColor:[UIColor colorWithRed:144.0/255.0 green:87.0/255.0 blue:192.0/255.0 alpha:1]];
        [_sexNan setBackgroundColor:[UIColor whiteColor]];
        _sexNan.selected = NO;
        [_sexNan setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_sexNv setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        setting.gender = SRGenderFemale;
    }
}

- (void)undertheFrame:(UIImage *)image
{
    _imageView.image = image;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y - 20);
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.center = CGPointMake(self.view.center.x, self.view.center.y + 20);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
    UITouch *touch = [touches anyObject];
    if (touch.view.tag == 1000) {
        PickerView *pickerView = [[PickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        [pickerView ageArea];
        pickerView.tag = 200;
        pickerView.delegete = self;
        if ([_age.text integerValue] == 0) {
            [pickerView defaultSelect:@"24"];
        }else
        {
            [pickerView defaultSelect:_age.text];
        }
        [self.view addSubview:pickerView];
    }else if (touch.view.tag == 1001) {
        PickerView *pickerView = [[PickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        [pickerView heightArea];
        pickerView.tag = 201;
        pickerView.delegete = self;
        if ([_height.text integerValue] == 0) {
            [pickerView defaultSelect:@"170"];
        }else
        {
            [pickerView defaultSelect:_height.text];
        }
        [self.view addSubview:pickerView];
    }else if (touch.view.tag == 1002) {
        PickerView *pickerView = [[PickerView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
        [pickerView weightArea];
        pickerView.tag = 202;
        pickerView.delegete = self;
        if ([_weight.text integerValue] == 0) {
            [pickerView defaultSelect:@"55"];
        }else
        {
            [pickerView defaultSelect:_weight.text];
        }
        [self.view addSubview:pickerView];
    }else if (touch.view.tag == 200 || touch.view.tag == 201 || touch.view.tag == 202)
    {
        [touch.view removeFromSuperview];
    }
}

#pragma mark -- 下一步
-(void)onRightButtonPressed
{
    SRViewSetting * setting = [SRViewSetting sharedSetting];
    if([_age.text integerValue] == 0)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"" message:@"请选择年龄" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
        return;
    }
    if(setting.height == 0)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"" message:@"请选择身高" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
        return;
    }
    if(setting.weight == 0)
    {
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"" message:@"请选择体重" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [alter show];
        return;
    }
    [self performSegueWithIdentifier:@"gotoChoose" sender:self];
}

- (void)pickerViewDelegatestr1:(NSString *)str1
{
    SRViewSetting * setting = [SRViewSetting sharedSetting];
    if ([str1 hasSuffix:@"岁"]) {
        _age.text = str1;
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy"];
        NSInteger temp = [[formatter stringFromDate:[NSDate date]] integerValue] - [str1 integerValue];
        NSString *str = [NSString stringWithFormat:@"%ld-01-01",temp];
        NSDate *date = [NSDate dateFromString:str];
        setting.birthday = date;
        
    }else if ([str1 hasSuffix:@"cm"])
    {
        _height.text = str1;
        setting.height = [str1 integerValue];
    }else if ([str1 hasSuffix:@"kg"])
    {
        _weight.text = str1;
        setting.weight = [str1 integerValue];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
